{include file="public/header" /}
{include file="public/nav" /}

<div class="row">
    <div class="col-md-8">
        <div class="page-header">
            <h2>{$title} </h2>
        </div>

        <form class="form-horizontal" method="post" id="login">
            <div class="form-group">
                <label for="userName" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" name="name" class="form-control" id="userName" placeholder="UserName">
                </div>
            </div>





            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input type="password" name="password" class="form-control" id="password" placeholder="Password">
                </div>
            </div>



            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button id="submit" type="button" class="btn btn-primary">登录</button>
                </div>
            </div>
            <div id=""><input id="token" type="hidden" name="__token__" value="{$Request.token}" /></div>
        </form>


    </div>

    <script>
        $(function () {
            $("#submit").on('click', function () {
                // alert($("#login").serialize());
                $.ajax({
                    type: 'post',
                    url: "{:url('index/user/loginCheck')}",
                    data: $("#login").serialize(),
                    dataType: 'json',
                    success: function (data) {
                        switch (data.status) {
                            case 1:
                                alert(data.message)
                                window.location.href="{:url('index/')}"
                                break
                            case 0:
                                alert(data.message)
                                retoken()
                                break
                            case -1:
                                alert(data.message)
                                retoken()
                                break
                            // window.location.back()
                            default:
                                retoken()
                                break
                        }

                    }
                })
            })

        })

        function retoken() {
            // console.log('err')
            let token = $("#token").val()
            get_token("{:url('index/user/get_token')}")
            console.log(token)

        }

        function get_token(url) {
            $.get(url,function (data) {
                ($("#token").val(data.token))
            },"json")

        }
    </script>

    {include file="public/right" /}

    {include file="public/footer" /}